package com.systop.controller;

import com.alibaba.excel.EasyExcel;
import com.systop.domain.vo.GoodsSalesVo;
import com.systop.domain.vo.SumSalesVo;
import com.systop.service.OrderService;
import com.systop.utils.ExcelUtil;
import com.systop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * @Description: TODO
 * @Author: jsj
 * @Date: 2023/3/28
 */
@RestController
public class ExcelController {

    @Autowired
    OrderService orderService;


    /**
     * 导出数据到新Excel文件
     * @param response
     * @param year
     * @param month
     * @return
     */
    @GetMapping("/exportAsExcel")
    public void exportAsExcel(HttpServletResponse response,String year,String month,Integer indexValue){
        //年份为空默认设置为本年度
        if(year == null || year.equals(""))
            year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));

        String fileName = year;
        if(month!=null && !month.equals("")){
            //单位数月份补0
            if(month.length()==1)
                month = "0"+month;
            fileName += "-" + month;
        }

        //请确保文件路径存在
        // String fileURL = fileName+"报表";
        String sheetName = fileName+"信息汇总";

        if(indexValue != null){
            switch (indexValue){
                case 0:break;
                case 1:sumSalesQuery(response,year,month,fileName,sheetName);
                       goodsSalesQuery(response,year,month,fileName,sheetName);
                       break;
                case 2:sumSalesQuery(response,year,month,fileName,sheetName);break;
                case 3:goodsSalesQuery(response,year,month,fileName,sheetName);break;
            }
        }
    }

    /**
     * 综合查询
     * @param response
     * @param year
     * @param month
     * @param fileURL
     * @param sheetName
     * @return
     */
    // private R synthesisQuery(HttpServletResponse response,String year,String month,String fileURL,String sheetName){
    //     //获取数据
    //     List<GoodsSalesVo> goodsSalesList = orderService.querySalesDetail(year,month);
    //     List<SumSalesVo> sumSalesList = orderService.querySalesGroupByMonth(year,month);
    //     //写入数据
    //     try {
    //         ExcelUtil.download(response, sumSalesList, fileURL+"总和报表", sheetName, SumSalesVo.class);
    //         ExcelUtil.download(response, goodsSalesList, fileURL+"商品报表", sheetName, GoodsSalesVo.class);
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //         return R.error("出现错误,请联系管理员");
    //     }
    //     return R.ok("已导出综合数据文档");
    // }

    /**
     * 总和查询
     * @param response
     * @param year
     * @param month
     * @param fileURL
     * @param sheetName
     * @return
     */
    private void sumSalesQuery(HttpServletResponse response,String year,String month,String fileURL,String sheetName){
        //获取数据
        List<SumSalesVo> sumSalesList = orderService.querySalesGroupByMonth(year,month);
        //写入数据
        try {
            ExcelUtil.download(response, sumSalesList, fileURL+"总和报表", sheetName, SumSalesVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 商品查询
     * @param response
     * @param year
     * @param month
     * @param fileURL
     * @param sheetName
     * @return
     */
    private void goodsSalesQuery(HttpServletResponse response,String year,String month,String fileURL,String sheetName){
        //获取数据
        List<GoodsSalesVo> goodsSalesList = orderService.querySalesDetail(year,month);
        //写入数据
        try {
            ExcelUtil.download(response, goodsSalesList, fileURL+"商品报表", sheetName, GoodsSalesVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
